//
//  WKHomeHttpTool.m
//  WKDemo
//
//  Created by apple on 14-8-1.
//  Copyright (c) 2014年 com.app. All rights reserved.
//首页数据请求网络工具类

#import "WKHomeHttpTool.h"
#import "WKHomeParam.h"
#import "MJExtension.h"
#import "WKHomeResult.h"
#import "WKResult.h"
#import "WKCommentResult.h"
#import "WKHttpTool.h"

@implementation WKHomeHttpTool

#pragma mark - 精华的请求
//获取首页默认列表也是下拉获取最新数据
+ (void)getEliteNewDataListWithParam:(WKHomeParam *)param success:(void (^)(WKHomeResult *))success failure:(void (^)(NSError *error))failure
{
    //将参数模型转字典
    NSDictionary *params = [param keyValues];
    
    [WKHttpTool get:@"http://api.budejie.com/api/api_open.php?market=baidu&maxid=&udid=862240020028782&a=list&c=data&page=1&per=20&visiting=&type=&time=week" params:params success:^(id responseObject) {
            NSString *maxid = responseObject[@"info"][@"maxid"];
            NSMutableArray *arrayM = [NSMutableArray array];
            [responseObject[@"list"] enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL *stop) {
                WKResult *r = [WKResult objectWithKeyValues:obj];
                [arrayM addObject:r];
            }];
            WKHomeResult *result =[[WKHomeResult alloc] init];
            result.homeDatas = arrayM;
            result.maxid = maxid;
            success(result);

    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}
//上拉加载数据
int a = 1;
+ (void)getEliteMoreDataListWithParam:(WKHomeParam *)param success:(void (^)(WKHomeResult *result))success failure:(void (^)(NSError *error))failure{
    NSString *maxid =param.maxid;
    a++;
  
    NSString *url = [NSString stringWithFormat:@"http://api.budejie.com/api/api_open.php?market=baidu&maxid=%@&udid=860642023466269&a=list&c=data&page=%d&per=20&visiting=&type=&time=week",maxid,a];
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    [mgr GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success && responseObject) {
           
            NSString *maxid = responseObject[@"info"][@"maxid"];
            NSMutableArray *arrayM = [NSMutableArray array];
            [responseObject[@"list"] enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL *stop) {
                WKResult *r = [WKResult objectWithKeyValues:obj];
                [arrayM addObject:r];
                
            }];
            WKHomeResult *result =[[WKHomeResult alloc] init];
            result.homeDatas = arrayM;
            result.maxid = maxid;
            success(result);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}
//获取评论
+ (void)getCommentDataListWithParam:(WKHomeParam *)param success:(void (^)(WKHomeResult *))success failure:(void (^)(NSError *error))failure{
    NSString *dataid = param.id;
    NSString *path = [NSString stringWithFormat:@"http://api.budejie.com/api/api_open.php?&market=baidu&udid=860642023466269&c=comment&a=dataList&data_id=%@&userID&per=20&page=1&hot=1",dataid];
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    [mgr GET:path parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            // WKLog(@"%@",responseObject);
            NSMutableArray *arrayM = [NSMutableArray array];
            [responseObject[@"data"] enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL *stop) {
                WKCommentResult *r = [WKCommentResult objectWithKeyValues:obj];
                r.username = obj[@"user"][@"username"];
                r.sex = obj[@"user"][@"sex"];
                r.profile_image = obj[@"user"][@"profile_image"];
                [arrayM addObject:r];
            }];
            WKHomeResult *result =[[WKHomeResult alloc] init];
            result.homeDatas = arrayM;
            
            success(result);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
    
}
#pragma mark - 最新的请求
//获取最新列表也是下拉获取最新数据
+ (void)getNewestNewDataListWithParam:(WKHomeParam *)param success:(void (^)(WKHomeResult *result))success failure:(void (^)(NSError *error))failure{
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    [mgr GET:@"http://api.budejie.com/api/api_open.php?market=baidu&maxid=&udid=862240020028781&a=newlist&c=data&userID=&page=1&per=20&visiting=&type=&time=week" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success == nil || responseObject == nil) return ;
            
            NSString *maxid = responseObject[@"info"][@"maxid"];
            NSMutableArray *arrayM = [NSMutableArray array];
            [responseObject[@"list"] enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL *stop) {
                WKResult *r = [WKResult objectWithKeyValues:obj];
                [arrayM addObject:r];
            
            }];
            WKHomeResult *result =[[WKHomeResult alloc] init];
            result.homeDatas = arrayM;
            result.maxid = maxid;
            
            success(result);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];

}
int b = 1;
//上拉加载数据
+ (void)getNewestMoreDataListWithParam:(WKHomeParam *)param success:(void (^)(WKHomeResult *result))success failure:(void (^)(NSError *error))failure{
    NSString *maxid =param.maxid;
    b++;
    
    NSString *url = [NSString stringWithFormat:@"http://api.budejie.com/api/api_open.php?market=baidu&max%@&udid=860642023466268&a=newlist&c=data&page=%d&per=20&visiting=&type=&time=week",maxid,b+1];
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    [mgr GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success && responseObject) {
            
            NSString *maxid = responseObject[@"info"][@"maxid"];
            NSMutableArray *arrayM = [NSMutableArray array];
            [responseObject[@"list"] enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL *stop) {
                WKResult *r = [WKResult objectWithKeyValues:obj];
                
                [arrayM addObject:r];
            }];
            WKHomeResult *result =[[WKHomeResult alloc] init];
            result.homeDatas = arrayM;
            result.maxid = maxid;
            success(result);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];

}
#pragma mark - 穿越的请求
//获取穿越列表也是下拉获取穿越最新数据
+ (void)getThrougNewDataListWithParam:(WKHomeParam *)param success:(void (^)(WKHomeResult *result))success failure:(void (^)(NSError *error))failure{
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    [mgr GET:@"http://api.budejie.com/api/api_open.php?market=baidu&order=timewarp&maxid=&udid=862240020028783&a=list&c=data&page=1&per=20&visiting=&type=&time=week" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success != nil && responseObject != nil) {
//            NSString *count = responseObject[@"info"][@"count"];
            
            NSString *maxid = responseObject[@"info"][@"maxid"];
            NSMutableArray *arrayM = [NSMutableArray array];
            [responseObject[@"list"] enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL *stop) {
                WKResult *r = [WKResult objectWithKeyValues:obj];
                [arrayM addObject:r];
            
            }];
            WKHomeResult *result =[[WKHomeResult alloc] init];
            result.homeDatas = arrayM;
            result.maxid = maxid;
            
            success(result);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}
int c = 1;
//上拉加载数据
+ (void)getThrougMoreDataListWithParam:(WKHomeParam *)param success:(void (^)(WKHomeResult *result))success failure:(void (^)(NSError *error))failure{
    NSString *maxid =param.maxid;
    c++;
    
    NSString *url = [NSString stringWithFormat:@"http://api.budejie.com/api/api_open.php?market=baidu&order=timewarp&max%@&udid=860642023466269&a=list&c=data&page=%d&per=20&visiting=&type=&time=week",maxid,c+1];
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    [mgr GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success != nil && responseObject != nil) {
            
            NSString *maxid = responseObject[@"info"][@"maxid"];
            NSMutableArray *arrayM = [NSMutableArray array];
            [responseObject[@"list"] enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL *stop) {
                WKResult *r = [WKResult objectWithKeyValues:obj];
                
                [arrayM addObject:r];
            }];
            WKHomeResult *result =[[WKHomeResult alloc] init];
            result.homeDatas = arrayM;
            result.maxid = maxid;
            
            success(result);
            
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];

}

//点赞
+ (void)sendClickLoveWithParam:(WKHomeParam *)param{
    NSString *di =param.id;
    NSString *path = [NSString stringWithFormat:@"http://api.budejie.com/api/api_open.php?&market=baidu&udid=860642023466269&c=data&a=love&dong=love&id=%@",di];
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    [mgr GET:path parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}
//点菜
+ (void)sendClickCaiWithParam:(WKHomeParam *)param{
    NSString *di =param.id;
    NSString *path = [NSString stringWithFormat:@"http://api.budejie.com/api/api_open.php?&market=baidu&udid=860642023466269&c=data&a=cai&dong=love&id=%@",di];
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    [mgr GET:path parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];

}
@end
